var ClubTableMemberUnit = Unit.extend({
    _ccsFileName: res.ClubTableMemberUnit_json,
    _popState: false,
    _clubId: null,
    _currentTabIsMember: false,
    _tableViewList: [],
    initUnit: function () {
        this._currentTabIsMember = true;
        this._nodeList['touchLayer'].setVisible(false);
        this._initTableView();
        this._setSeelctTab('tab_left');
    },
    /**
     * View基类初始化
     */
    initView: function () {
        this._actionType = ViewActionType.None;
        if (this._actionType === ViewActionType.None) {
            this._rootNode.show();
        }
        this._loadCCSFinished = true;
        this.setData.apply(this, arguments);
    },
    onEnter: function () {
        Unit.prototype.onEnter.call(this);
        this.x = 0;
        this._popState = false;
        this._initAllListeners();
    },
    /**
     * 监听亲友圈消息
     * @private
     */
    _initEventListeners: function () {
        this._eventListeners = {};
        this._eventListeners[ClubMsgCode.AllMemberInfo] = '_allMemberInfoHandler';
        this._eventListeners[ClubMsgCode.ListClubRoom] = '_listClubRoomHandler';

        this._eventListeners[ClubEvent.ClubBaseInfo] = '_clubBaseInfoHandler';
        this._eventListeners[ClubEvent.ClubCloseTableMemeberUnit] = '_cloesTableHandler';
    },


    _initClickListeners: function () {
        this._clickListeners = {
            'btn_popOnLine': '_clickPop',
            'tab_left': '_cliclTab',
            'tab_right': '_cliclTab',
            'touchLayer': '_clickTouchLayer',
            'tableView': '_clickTableView',
        }
    },

    /**
     * 初始化TableView
     * @private
     */
    _initTableView: function () {
        var nodeContent = this._nodeList['tableView'];
        var tableViewSize = nodeContent.getContentSize();
        this._tableView = new cc.TableView(this, cc.size(tableViewSize.width, tableViewSize.height));
        this._tableView.setDelegate(this);
        this._tableView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
        this._tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
        nodeContent.addChild(this._tableView);
    },

    _clubBaseInfoHandler: function (clubId) {
        this._clubId = clubId;
    },
    _cloesTableHandler: function () {
        if (this._popState) {
            this._clickPop();
        }
    },

    _allMemberInfoHandler: function (data) {
        if (this._currentTabIsMember) {
            this._refreshMembersView();
        }
    },

    _listClubRoomHandler: function () {
        if (!this._currentTabIsMember) {
            this._refreshRoomView();
        }
    },


    _clickTouchLayer: function () {
        this._clickPop();
    },
    _clickTableView: function () {
    },

    _clickPop: function () {
        var moveWidth = 680;
        if (this._popState) {
            // moveWidth = -moveWidth;
            moveWidth = 0;
        }
        this._popState = !this._popState;
        this._nodeList['touchLayer'].setVisible(this._popState);
        this.runAction(cc.moveTo(0.5, cc.p(moveWidth, 0)));
        if (this._popState) {
            clubMsgManager.requestClubMember(this._clubId);
            clubMsgManager.requestListClubRoom(this._clubId);
        }
    },
    _cliclTab: function (target) {
        this._setSeelctTab(target.getName());
    },
    _setSeelctTab: function (selectName) {
        this._nodeList['tab_left.tab_select'].setVisible(selectName == 'tab_left');
        this._nodeList['memberPanel'].setVisible(selectName == 'tab_left');

        this._nodeList['tab_right.tab_select'].setVisible(selectName == 'tab_right');
        this._nodeList['tablePanel'].setVisible(selectName == 'tab_right');
        this._currentTabIsMember = selectName == 'tab_left';
        if (this._currentTabIsMember) {
            this._refreshMembersView();
        } else {
            this._refreshRoomView();
        }
    },


    _refreshMembersView: function () {
        var members = ClubDataManager.getClubMembers(this._clubId);
        this._tableViewList = [];
        for (var i = 0; i < members.length; i++) {
            if (members[i]['state'] == ClubDataManager.MemberState_OffLine) {
                continue;
            }
            this._tableViewList.push(members[i]);
        }
        this._tableView.reloadData();
    },

    _refreshRoomView: function () {
        var roomList = ClubDataManager.getClubFloorRomList(this._clubId);
        this._tableViewList = ClubDataManager.getFormatClubRoomList(roomList, ClubTableItemType.Type_WaitJoin);
        this._tableView.reloadData();
    },

    tableCellAtIndex: function (table, idx) {
        var cell = table.dequeueCell();
        var node = null;
        if (cell == null) {
            cell = new cc.TableViewCell();
            node = addItem('ClubTableMemberUnitItem', cell, (idx + 1), this._tableViewList[idx], this._currentTabIsMember);
            node.setName("itemCell");
        } else {
            node = cell.getChildByName("itemCell");
            node.setData((idx + 1), this._tableViewList[idx], this._currentTabIsMember);
        }
        return cell;
    },
    tableCellTouched: function (table, cell) {
    },
    tableCellSizeForIndex: function (table, idx) {
        return cc.size(630, 50);
    },
    numberOfCellsInTableView: function (table) {
        return this._tableViewList.length;
    },

});